import { defHttp } from '/@/utils/http/axios';
import { ResultEnum } from '/@/enums/httpEnum';
import { ErrorMessageMode } from '/#/axios';

import {
  ModelListItem,
  ModelListGetResultModel,
  DBTableItem,
  OptionParams,
  OptionItem,
  UploadApiResult,
} from './model';
import { UploadFileParams } from '/#/axios';

export enum MODEL_TYPE {
  LIST = 1,
  FORM,
  COMPLEX,
}
export enum FORM_GET_DATA_TYPE {
  DEFAULT = 1,
  READONLY = 2,
  PART_READONLY = 3,
}
export interface ModelParams {
  moduleId?: string | number;
  modelId?: string | number;
  [key: string]: any;
}

export type ModelIds = Pick<ModelParams, 'moduleId' | 'modelId'>;

enum Api {
  AddTABLE_MODEL = '/model/tablemodel/add',
  DelTABLE_MODEL = '/model/tablemodel/del',
  UpdateTABLE_MODEL = '/model/tablemodel/update',
  ImportTABLE_MODEL = '/model/tablemodel/import',
  TABLE_MODEL = '/model/tablemodel',
  TABLE_INSTANCE = '/model/tablemodel/instance',
  ALL_MODEL = '/model/tablemodel/list',
  MODEL_PAGE = '/model/tablemodel/page',
  FIELDS_FROM_DB = '/model/tablemodel/getFieldsFromDb', // 库表定义
  GetTablesFromDb = '/model/tablemodel/getTablesFromDb',
  FIELDS_FROM_TABLE_DEFINE = '/model/tablemodel/getFieldsFromTableDefine', // 定义表
  GetModuleCommonList = '/admin/module/commonList/',
  GetModuleCommonListPreview = '/admin/module/commonListPreview',
  GetModuleListlnfo = '/admin/module/getModuleListInfo/',
  GetCompositeInfo = '/admin/module/getModuleCompositeInfo/',
  TableModelTypeTree = '/admin/tablemodeltype/treeOption',
  UserProfileSave = '/admin/userProfile/save', // 表格列位置配置
  ModuleFormInfo = '/admin/module/getModuleFormInfo/',
  TaskCaliberOperate = '/jd/task/caliberOperate',
}

enum FormApi {
  BTN_ADD = '/admin/module/commonAdd/',
  BTN_UPDATE = '/admin/module/commonUpdate/',
  BTN_INFO = '/admin/module/commonGetById/',
  BTN_DEL = '/admin/module/commonRemove/',
  BTN_BATCH_DEL = '/admin/module/commonBatchRemove/',
  LIST_ROW_SWITCH = '/admin/module/commonSwitch/',
  EXPORT = '/admin/module/commonExport/',
  STATUS_ROW_SWITCH = '/admin/module/updateStatus/',
}

enum ModuleApi {
  VERIFY_DATA_UNIQUE = '/model/module/verifyDataUnique',
  COM_SELECT = '/model/module/commonFormSelect',
  COM_TREE_SELECT = '/model/module/commonFormTree',
  COM_UPLOAD = '/model/module/commonFileUpload',
  COM_UPLOAD_DEL = '/model/module/commonDeleteFile',
  COM_UPLOAD_REMINDING = '/admin/sys-file/upload',
  SAVE_FORM_FIELD_AUTH = '/model/tablemodel/addFormPermission/',
  GET_FORM_FIELD_AUTH = '/model/tablemodel/getFormPermissionList/',
  COMMONFILE_PREVIEW = '/model/module/commonFilePreview',
}

const getID = ({ moduleId, modelId }: ModelIds) => {
  return `${moduleId}/${modelId}`;
};

export const commonVerifyDataUniqueApi = (params: {
  modelId: string;
  field: string;
  content: string;
  oldContent: string;
}) =>
  defHttp.post<any>({
    url: ModuleApi.VERIFY_DATA_UNIQUE,
    params,
  });

export const statusSwitchApi = (ids: ModelIds, params?: any) =>
  defHttp.post<any>({
    url: FormApi.STATUS_ROW_SWITCH + getID(ids),
    params,
  });

export const commonFilePreviewApi = (filePath: string) =>
  defHttp.post<any>({
    url: ModuleApi.COMMONFILE_PREVIEW,
    params: {
      filePath,
    },
  });

export const commonExportApi = (ids: ModelIds, params: ModelParams) =>
  defHttp.post<any>({
    url: FormApi.EXPORT + getID(ids),
    params,
  });

export const commonGetByIdApi = (ids: ModelIds, params: ModelParams, readType = 1) => {
  return defHttp.post<any>({
    url: FormApi.BTN_INFO + getID(ids) + `?type=${readType}`,
    params,
  });
};
//  表单模型对应值的数据
export const commonGetModelIdByIdApi = (ids: ModelIds, params: ModelParams, readType) => {
  return defHttp.post<any>({
    url: FormApi.BTN_INFO + ids.modelId + `?type=${readType || 1}`,
    params,
  });
};

export const commonAddApi = (ids: ModelIds, params: ModelParams) =>
  defHttp.post<any>({
    url: FormApi.BTN_ADD + getID(ids),
    params,
  });

export const commonUpdateApi = (ids: ModelIds, params: ModelParams) =>
  defHttp.post<any>({
    url: FormApi.BTN_UPDATE + getID(ids),
    params,
  });

export const commonRemoveApi = (
  ids: ModelIds,
  params?: ModelParams,
  type: ErrorMessageMode = 'msg',
) =>
  defHttp.post<any>(
    {
      url: FormApi.BTN_DEL + getID(ids),
      params,
    },
    { errorMessageMode: type },
  );

export const commonBatchApi = (ids: ModelIds, params: ModelParams) =>
  defHttp.post<any>({
    url: FormApi.BTN_BATCH_DEL + getID(ids),
    params,
  });

export const commonSwitchApi = (ids: ModelIds, params?: ModelParams) =>
  defHttp.post<any>({
    url: FormApi.LIST_ROW_SWITCH + getID(ids),
    params,
  });

export const getDBTableNamesApi = (params?: DBTableItem) =>
  defHttp.get<any>({
    url: Api.GetTablesFromDb,
    params: {
      size: 50,
      current: 1,
      ...params,
    },
  });

export const getFieldsFromTableDefineListApi = (params?: DBTableItem) =>
  defHttp.get<ModelListItem[]>({
    url: Api.FIELDS_FROM_TABLE_DEFINE,
    params,
  });

export const getFieldsFromDbListApi = (params: Indexable) =>
  defHttp.get<ModelListItem[]>({
    url: Api.FIELDS_FROM_DB,
    params,
  });

export const getModelAllListApi = (params?: ModelListItem) =>
  defHttp.get<ModelListItem[]>({
    url: Api.ALL_MODEL,
    params: {
      classify: MODEL_TYPE.LIST,
      ...params,
    },
  });

export const getModelAllFormApi = (params?: ModelListItem) =>
  defHttp.get<ModelListItem>({
    url: Api.ALL_MODEL,
    params: {
      classify: MODEL_TYPE.FORM,
      ...params,
    },
  });

export const saveModelApi = <T = ModelListItem>(params?: T) =>
  defHttp.post<any>({ url: Api.AddTABLE_MODEL, params });

export const updateModelApi = <T = ModelListItem>(params?: T) =>
  defHttp.post<any>({ url: Api.UpdateTABLE_MODEL, params });

export const importModelApi = (params: ModelListItem) =>
  defHttp.post<any>({ url: Api.ImportTABLE_MODEL, params });

export const getModelInfoApi = <T = ModelListItem>(id: string) =>
  defHttp.get<T>({ url: Api.TABLE_MODEL + '/' + id });

// 模型配置数据
export const getModelInstanceInfoApi = (id: string, data?: Indexable) => {
  return defHttp.post<ModelListItem>({ url: Api.TABLE_INSTANCE + '/' + id, data });
};

export const delModelApi = (id: string) =>
  defHttp.post<any>({ url: Api.DelTABLE_MODEL + '/' + id });

export const getModelByListApi = (params?: ModelListItem) =>
  defHttp.get<ModelListGetResultModel>({
    url: Api.MODEL_PAGE,
    params: {
      classify: '1',
      ...params,
    },
  });
export const getModelByFormListApi = (params?: ModelListItem) =>
  defHttp.get<ModelListGetResultModel>({
    url: Api.MODEL_PAGE,
    params: {
      classify: '2',
      ...params,
    },
  });
export const getModelByComplexListApi = (params?: ModelListItem) =>
  defHttp.get<ModelListGetResultModel>({
    url: Api.MODEL_PAGE,
    params: {
      classify: '3',
      ...params,
    },
  });
export const getTableModelTypeTreeApi = () => defHttp.get<any>({ url: Api.TableModelTypeTree });

// 表单模型
export const getModuleFormInfoApi = (ids: ModelIds) =>
  defHttp.get<ModelListItem>({ url: Api.ModuleFormInfo + getID(ids) });

// 列表模型
export const getModuleListInfoApi = (ids: ModelIds) =>
  defHttp.get<any>({ url: Api.GetModuleListlnfo + getID(ids) });

//  复合模型
export const getCompositeInfoApi = (ids: ModelIds) =>
  defHttp.get<any>({ url: Api.GetCompositeInfo + getID(ids) });

// 列表数据
export const getGetModuleCommonListApi = (ids: ModelIds, params: ModelParams) =>
  defHttp.post<any>({ url: Api.GetModuleCommonList + getID(ids), params });

// 列表数据预览
export const getGetModuleCommonListPreviewApi = (modelId: string, params: ModelParams) =>
  defHttp.post<any>({ url: Api.GetModuleCommonListPreview + '/' + modelId, params });

export const getOptionsForSelectApi = (params?: OptionParams) =>
  defHttp.post<OptionItem[]>({
    url: ModuleApi.COM_SELECT,
    params,
  });

export const getOptionsForTreeSelectApi = (params?: OptionParams) =>
  defHttp.post<OptionItem[]>({
    url: ModuleApi.COM_TREE_SELECT,
    params,
  });

export const delFileApi = (params: Indexable) =>
  defHttp.post<any[]>({
    url: ModuleApi.COM_UPLOAD_DEL,
    params,
  });
export const comFileUploadApi = async (
  params: UploadFileParams,
  onUploadProgress: (progressEvent: ProgressEvent) => void,
) => {
  const ret = await defHttp.uploadFile<UploadApiResult>(
    {
      url: ModuleApi.COM_UPLOAD,
      onUploadProgress,
    },
    params,
  );

  const { data } = ret;
  const hasSuccess = data && Reflect.has(data, 'code') && data.code === ResultEnum.SUCCESS;
  if (hasSuccess) {
    return data;
  } else {
    Promise.reject(data);
    return data;
  }
};
export const comFileUploadRemindingApi = async (
  params: UploadFileParams,
  onUploadProgress: (progressEvent: ProgressEvent) => void,
) => {
  const ret = await defHttp.uploadFile<UploadApiResult>(
    {
      url: ModuleApi.COM_UPLOAD_REMINDING,
      onUploadProgress,
    },
    params,
  );

  const { data } = ret;
  const hasSuccess = data && Reflect.has(data, 'code') && data.code === ResultEnum.SUCCESS;
  if (hasSuccess) {
    return data;
  } else {
    Promise.reject(data);
    return data;
  }
};

export const saveFormFieldAuthApi = (moduleId: string | number, params?: Indexable) =>
  defHttp.post<any>({
    url: ModuleApi.SAVE_FORM_FIELD_AUTH + moduleId,
    params,
  });

export const getFormFieldAuthApi = (moduleId: string | number, params?: Indexable) =>
  defHttp.get<any>({
    url: ModuleApi.GET_FORM_FIELD_AUTH + moduleId,
    params,
  });

export const saveUserProfileApi = (params: ModelParams) =>
  defHttp.post<any>({
    url: Api.UserProfileSave,
    params,
  });

export const getTaskCaliberOperate = (params: { id: number; type: string }) => {
  defHttp.get<any>({
    url: Api.TaskCaliberOperate + params.id + params.type,
  });
};
